import SwiftUI

struct GeocodingConfigView: View {
    @StateObject private var config = GeocodingConfig.shared
    @State private var showingAlert = false
    @State private var alertMessage = ""
    
    var body: some View {
        NavigationView {
            Form {
                // 服务配置部分
                Section(header: Text("服务配置")) {
                    // 主服务选择
                    Picker("主服务", selection: $config.primaryService) {
                        ForEach(GeocodingService.allCases, id: \.self) { service in
                            Text(service.displayName).tag(service)
                        }
                    }
                    .onChange(of: config.primaryService) { _ in
                        config.saveConfig()
                    }
                    
                    // 备用服务选择
                    Picker("备用服务", selection: $config.secondaryService) {
                        ForEach(GeocodingService.allCases, id: \.self) { service in
                            Text(service.displayName).tag(service)
                        }
                    }
                    .onChange(of: config.secondaryService) { _ in
                        config.saveConfig()
                    }
                }
                
                // 服务开关部分
                Section(header: Text("服务开关")) {
                    Toggle("启用Apple地理编码", isOn: $config.appleEnabled)
                        .onChange(of: config.appleEnabled) { _ in
                            config.saveConfig()
                        }
                    
                    Toggle("启用高德地图API", isOn: $config.amapEnabled)
                        .onChange(of: config.amapEnabled) { _ in
                            config.saveConfig()
                        }
                    
                    Toggle("启用Google地图API", isOn: $config.googleEnabled)
                        .onChange(of: config.googleEnabled) { _ in
                            config.saveConfig()
                        }
                    
                    Toggle("启用OpenStreetMap", isOn: $config.openStreetMapEnabled)
                        .onChange(of: config.openStreetMapEnabled) { _ in
                            config.saveConfig()
                        }
                }
                
                // 重试配置部分
                Section(header: Text("重试配置")) {
                    Stepper("最大重试次数: \(config.maxRetries)", value: $config.maxRetries, in: 0...5)
                        .onChange(of: config.maxRetries) { _ in
                            config.saveConfig()
                        }
                    
                    HStack {
                        Text("重试延迟")
                        Spacer()
                        TextField("延迟秒数", value: $config.retryDelay, format: .number)
                            .keyboardType(.decimalPad)
                            .frame(width: 80)
                            .textFieldStyle(RoundedBorderTextFieldStyle())
                            .onChange(of: config.retryDelay) { _ in
                                config.saveConfig()
                            }
                        Text("秒")
                    }
                }
                
                // 快速配置部分
                Section(header: Text("快速配置")) {
                    Button("Apple优先") {
                        config.primaryService = .apple
                        config.secondaryService = .amap
                        config.saveConfig()
                        showAlert("已设置为Apple地理编码优先")
                    }
                    .foregroundColor(.blue)
                    
                    Button("高德优先") {
                        config.primaryService = .amap
                        config.secondaryService = .google
                        config.saveConfig()
                        showAlert("已设置为高德地图API优先")
                    }
                    .foregroundColor(.blue)
                    
                    Button("仅使用Apple") {
                        config.appleEnabled = true
                        config.amapEnabled = false
                        config.googleEnabled = false
                        config.openStreetMapEnabled = false
                        config.saveConfig()
                        showAlert("已设置为仅使用Apple地理编码")
                    }
                    .foregroundColor(.green)
                    
                    Button("仅使用高德") {
                        config.appleEnabled = false
                        config.amapEnabled = true
                        config.googleEnabled = false
                        config.openStreetMapEnabled = false
                        config.saveConfig()
                        showAlert("已设置为仅使用高德地图API")
                    }
                    .foregroundColor(.green)
                    
                    Button("重置为默认") {
                        config.resetConfig()
                        showAlert("已重置为默认配置")
                    }
                    .foregroundColor(.orange)
                }
                
                // 配置状态部分
                Section(header: Text("配置状态")) {
                    let isValid = config.validateConfig()
                    
                    HStack {
                        Image(systemName: isValid ? "checkmark.circle.fill" : "xmark.circle.fill")
                            .foregroundColor(isValid ? .green : .red)
                        Text(isValid ? "配置有效" : "配置无效")
                            .foregroundColor(isValid ? .primary : .red)
                    }
                    
                    let availableServices = config.getAvailableServices()
                    Text("可用服务: \(availableServices.count) 个")
                        .foregroundColor(.secondary)
                    
                    ForEach(availableServices, id: \.self) { service in
                        HStack {
                            Image(systemName: "arrow.right")
                                .foregroundColor(.blue)
                            Text(service.displayName)
                        }
                    }
                }
                
                // 测试部分
                Section(header: Text("测试配置")) {
                    Button("测试当前配置") {
                        testCurrentConfiguration()
                    }
                    .foregroundColor(.purple)
                }
            }
            .navigationTitle("地理编码配置")
            .alert("配置更新", isPresented: $showingAlert) {
                Button("确定") { }
            } message: {
                Text(alertMessage)
            }
        }
    }
    
    private func showAlert(_ message: String) {
        alertMessage = message
        showingAlert = true
    }
    
    private func testCurrentConfiguration() {
        let isValid = config.validateConfig()
        if isValid {
            showAlert("配置有效，可以开始使用")
        } else {
            showAlert("配置无效，请检查设置")
        }
    }
}

#Preview {
    GeocodingConfigView()
} 